import json
from django.test import RequestFactory, TestCase, Client
from rest_framework import status

from juqiadmin.models import JuqiAdmin, get_game_finance

from yifanshang.models import YifanshangGame, YifanshangSonGame, YifanshangGameGood

from juqiadmin.models import Finance, get_game_finance
from wxuser.models import Users
from warehouse.models import JuqiGood, GoodLevel, GoodLevelGameType


def save_data():
    chizi = YifanshangGame.objects.create(
        name="test池子",
        cover_url="123",
        price=58,
        raffle_counts='["抽一次","抽三次","抽五次","抽十次","全收"]',
        game_num=5,
        state=1,
        is_overlord=1,
        overlord_second=60,
    )

    son_game_list = []
    for v in range(1, 5 + 1):
        son_game_list.append(
            YifanshangSonGame(
                **{
                    "state": 1,
                    "game_uuid": chizi.uuid,
                    "price": 58,
                    "raffle_count": 16,
                    "remaining_raffle_count": 16,
                    "box_num": v,
                }
            )
        )
    YifanshangSonGame.objects.bulk_create(son_game_list)

    son_game = YifanshangSonGame.objects.filter(game_uuid=chizi.uuid).first()

    good_level_last = GoodLevel.objects.create(
        game_type=GoodLevelGameType.yifanshang.value,
        title="最终赏",
        method_type=1,
        img_url="123",
    )
    good_level_a = GoodLevel.objects.create(
        game_type=GoodLevelGameType.yifanshang.value,
        title="A赏",
        method_type=0,
        img_url="123",
    )
    good_level_b = GoodLevel.objects.create(
        game_type=GoodLevelGameType.yifanshang.value,
        title="B赏",
        method_type=0,
        img_url="123",
    )
    good_level_c = GoodLevel.objects.create(
        game_type=GoodLevelGameType.yifanshang.value,
        title="C赏",
        method_type=0,
        img_url="123",
    )
    good_level_d = GoodLevel.objects.create(
        game_type=GoodLevelGameType.yifanshang.value,
        title="D赏",
        method_type=0,
        img_url="123",
    )

    jq_good_lf = JuqiGood.objects.create(
        name="路飞",
        display_url="123",
        price=58,
        recovery_price=180,
        game_type="一番赏",
        ip_family="海贼王",
        # 商品详细介绍
        detail="123",
    )
    jq_good_nm = JuqiGood.objects.create(
        name="娜美",
        display_url="123",
        price=58,
        recovery_price=55,
        game_type="一番赏",
        ip_family="海贼王",
        # 商品详细介绍
        detail="123",
    )
    jq_good_zl = JuqiGood.objects.create(
        name="佐罗",
        display_url="123",
        price=58,
        recovery_price=55,
        game_type="一番赏",
        ip_family="海贼王",
        # 商品详细介绍
        detail="123",
    )
    jq_good_sz = JuqiGood.objects.create(
        name="山治",
        display_url="123",
        price=58,
        recovery_price=55,
        game_type="一番赏",
        ip_family="海贼王",
        # 商品详细介绍
        detail="123",
    )
    jq_good_qb = JuqiGood.objects.create(
        name="乔巴",
        display_url="123",
        price=58,
        recovery_price=20,
        game_type="一番赏",
        ip_family="海贼王",
        # 商品详细介绍
        detail="123",
    )

    YifanshangGameGood.objects.create(
        game_uuid=son_game.uuid,
        good_uuid=jq_good_lf.uuid,
        good_level_uuid=good_level_last.uuid,
        good_type=0,
        good_desc_url="123",
        good_presale_type="现货",
        count=1,
        current_count=1,
    )
    YifanshangGameGood.objects.create(
        game_uuid=son_game.uuid,
        good_uuid=jq_good_nm.uuid,
        good_level_uuid=good_level_a.uuid,
        good_type=1,
        good_desc_url="123",
        good_presale_type="现货",
        count=1,
        current_count=1,
    ),
    YifanshangGameGood.objects.create(
        game_uuid=son_game.uuid,
        good_uuid=jq_good_zl.uuid,
        good_level_uuid=good_level_b.uuid,
        good_type=1,
        good_desc_url="123",
        good_presale_type="现货",
        count=2,
        current_count=2,
    ),
    YifanshangGameGood.objects.create(
        game_uuid=son_game.uuid,
        good_uuid=jq_good_sz.uuid,
        good_level_uuid=good_level_c.uuid,
        good_type=1,
        good_desc_url="123",
        good_presale_type="现货",
        count=3,
        current_count=3,
    ),
    YifanshangGameGood.objects.create(
        game_uuid=son_game.uuid,
        good_uuid=jq_good_qb.uuid,
        good_level_uuid=good_level_d.uuid,
        good_type=1,
        good_desc_url="123",
        good_presale_type="现货",
        count=10,
        current_count=10,
    )

    return chizi.uuid, son_game.uuid


class YfsGameStateView(TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.c = Client()
        self.factory = RequestFactory()
        self.admin_user = {"user": "admin", "password": "admin123"}
        u = JuqiAdmin(**self.admin_user)
        u.save()

    def test_update_game_state(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)
        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        game_uuid, _ = save_data()
        # 上架
        r = self.c.put(
            "/api/yifanshang/game/state",
            {"game_uuid": game_uuid, "state": 1},
            content_type="application/json",
        )
        res = json.loads(r.content)
        self.assertEqual(res["code"], 0)

        # 下架
        r = self.c.put(
            "/api/yifanshang/game/state",
            {"game_uuid": game_uuid, "state": 0},
            content_type="application/json",
        )
        res = json.loads(r.content)
        self.assertEqual(res["code"], 0)


class YfsGameView(TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.c = Client()
        self.factory = RequestFactory()
        self.admin_user = {"user": "admin", "password": "admin123"}
        u = JuqiAdmin(**self.admin_user)
        u.save()

    def test_get_game(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)
        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        save_data()

        r = self.c.get(
            "/api/yifanshang/game",
        )
        res = json.loads(r.content)
        self.assertEqual(res["code"], 0)

    def test_create_yfs(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)
        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        r = self.c.post(
            "/api/yifanshang/game",
            {
                "name": "火影忍者",
                "cover_url": "http://ss",
                "price": 58,
                "game_num": 5,
                "pay_type": 100,
                "raffle_counts": '["抽一次", "抽三次", "抽三次", "霸主模式"]',
            },
            content_type="application/json",
        )
        res = json.loads(r.content)
        self.assertEqual(res["code"], 0)

    def test_put_yfs(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)
        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        chizi = YifanshangGame.objects.create(
            name="test池子",
            cover_url="123",
            price=58,
            game_num=5,
            pay_type=100,
            raffle_counts='["抽一次", "抽三次", "抽三次", "霸主模式"]',
        )

        r = self.c.put(
            "/api/yifanshang/game",
            {
                "game_uuid": chizi.uuid,
                "cover_url": "http://ss",
                "price": 68,
            },
            content_type="application/json",
        )
        res = json.loads(r.content)
        self.assertEqual(res["code"], 0)

    def test_del_yfs(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)
        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        game_uuid, _ = save_data()

        r = self.c.delete(
            "/api/yifanshang/game",
            {
                "game_uuid": game_uuid,
            },
            content_type="application/json",
        )
        res = json.loads(r.content)
        self.assertEqual(res["code"], 0)


class YfsSonGameView(TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.c = Client()
        self.factory = RequestFactory()
        self.admin_user = {"user": "admin", "password": "admin123"}
        u = JuqiAdmin(**self.admin_user)
        u.save()

    def test_get_son_game_list(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)
        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        game_uuid = save_data()
        r = self.c.get(
            "/api/yifanshang/songame",
            {"game_uuid": game_uuid, "page": 1, "page_size": 30},
            content_type="application/json",
        )
        res = json.loads(r.content)
        self.assertEqual(res["code"], 0)


class YfsGoodView(TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.c = Client()
        self.factory = RequestFactory()
        self.admin_user = {"user": "admin", "password": "admin123"}
        u = JuqiAdmin(**self.admin_user)
        u.save()

    def test_get_good_list(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)
        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        game_uuid, _ = save_data()

        get_good = self.c.get("/api/yifanshang/good", {"game_uuid": game_uuid})

        get_good_res = json.loads(get_good.content)
        self.assertEqual(get_good_res["code"], 0)

        good_detail = self.c.get(
            "/api/yifanshang/good/detail", {"uuid": get_good_res["data"][0]["uuid"]}
        )
        good_detail_res = json.loads(good_detail.content)
        print(good_detail_res, "guoqingggg")
        self.assertEqual(good_detail_res["code"], 0)

    def test_good_bind_game(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)

        d = json.loads(r.content)
        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        game_uuid, _ = save_data()
        good_level_e = GoodLevel.objects.create(
            game_type=GoodLevelGameType.yifanshang.value,
            title="E赏",
            method_type=0,
            img_url="123",
        )
        r = self.c.post(
            "/api/yifanshang/good",
            {
                "game_uuid": game_uuid,
                "name": "弗兰克",
                "ip_family": "海贼王",
                "display_url": "https://1234",
                "img_desc_url": "商品详情图url",
                "price": 58,
                "recovery_price": 38,
                "detail": "商品描述",
                "good_level_uuid": good_level_e.uuid,
                "good_type": 1,
                "good_presale_time": "2023-03-15",
                "count": 1,
            },
            content_type="application/json",
        )
        res = json.loads(r.content)
        self.assertEqual(res["code"], 0)


class YfsRaffleRecordView(TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.c = Client(HTTP_X_WX_OPENID="wx123")
        self.factory = RequestFactory()

    def test_get_yfs_raffle_record_no(self):
        XcxYfsSonGameDetail.create_a_user()

        game_uuid, son_game_uuid = save_data()

        r = self.c.post(
            "/api/yifanshang/xcx/songame",
            {
                "game_uuid": game_uuid,
                "son_game_uuid": son_game_uuid,
                "pay_type": "hunjing",
                "raffle_count": 10,
            },
            content_type="application/json",
        )

        raffle_r = self.c.get("/api/yifanshang/raffle", {"uuid": son_game_uuid})
        raffle_res = json.loads(raffle_r.content)
        self.assertEqual(raffle_res["code"], 0)


class YfsFinanceView(TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.c = Client()
        self.factory = RequestFactory()
        self.admin_user = {"user": "admin", "password": "admin123"}
        u = JuqiAdmin(**self.admin_user)
        u.save()

    def test_game_finance(self):
        r = self.c.post(
            "/api/admin/login",
            self.admin_user,
            content_type="application/json",
        )
        self.assertEqual(r.status_code, status.HTTP_200_OK)
        d = json.loads(r.content)
        self.assertEqual(len(self.c.session.get("login_token", "")), 64)
        self.assertEqual(d["code"], "0")

        game_uuid, son_game_uuid = save_data()
        Finance.objects.create(
            user_uuid=game_uuid,
            all_raffle_income=1000,
            all_raffle_cost=800,
            all_raffle_profit=200,
        )
        Finance.objects.create(
            user_uuid=son_game_uuid,
            all_raffle_income=1000,
            all_raffle_cost=800,
            all_raffle_profit=200,
        )

        finance_r = self.c.get(
            "/api/yifanshang/finance",
            {
                "game_uuid": game_uuid,
            },
        )
        finance_res = json.loads(finance_r.content)
        self.assertEqual(finance_res["code"], 0)

        finance_r = self.c.get(
            "/api/yifanshang/finance",
            {
                "game_uuid": son_game_uuid,
            },
        )
        finance_res = json.loads(finance_r.content)
        self.assertEqual(finance_res["code"], 0)


class XcxYfsGameView(TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.c = Client(HTTP_X_WX_OPENID="wx123")
        self.factory = RequestFactory()

    def test_xcx_get_yfs_game(self):
        XcxYfsSonGameView.create_a_user()

        _, _ = save_data()
        game_r = self.c.get(
            "/api/yifanshang/xcx/game",
        )
        game_res = json.loads(game_r.content)
        self.assertEqual(game_res["code"], 0)


class XcxYfsSonGameView(TestCase):
    def setUp(self):
        self.c = Client(HTTP_X_WX_OPENID="wx123")
        self.factory = RequestFactory()

    @classmethod
    def create_a_user(cls):
        xiaoming_user = (
            Users.objects.filter(deleted=False).filter(nickname="xiaoming").first()
        )
        if xiaoming_user is not None:
            return xiaoming_user

        d = {
            "nickname": "xiaoming",
            "head_url": "http://nick",
            "openid": "wx123",
            "stored_money": 1000000,
            "score": 0,
        }
        u = Users(**d)
        u.save()
        return u

    def test_get_game_songame(self):
        game_uuid, _ = save_data()
        XcxYfsSonGameView.create_a_user()

        game_detail_r = self.c.get(
            "/api/yifanshang/xcx/game/detail", {"uuid": game_uuid}
        )
        game_detail_res = json.loads(game_detail_r.content)
        self.assertEqual(game_detail_res["code"], 0)


class XcxYfsSonGameListView(TestCase):
    def setUp(self):
        self.c = Client(HTTP_X_WX_OPENID="wx123")
        self.factory = RequestFactory()

    @classmethod
    def create_a_user(cls):
        d = {
            "nickname": "xiaoming",
            "head_url": "http://nick",
            "openid": "wx123",
            "stored_money": 200,
            "hunjing": 500,
            "score": 1000000,
        }
        u = Users(**d)
        u.save()
        return u

    def test_get_son_game_list(self):
        XcxYfsSonGameListView.create_a_user()
        game_uuid, _ = save_data()
        get_son_game_list_r = self.c.get(
            "/api/yifanshang/xcx/songame", {"game_uuid": game_uuid}
        )
        get_son_game_list_res = json.loads(get_son_game_list_r.content)
        self.assertEqual(get_son_game_list_res["code"], 0)

    def test_son_game_raffle(self):
        XcxYfsSonGameListView.create_a_user()

        game_uuid, son_game_uuid = save_data()

        raffle_r = self.c.post(
            "/api/yifanshang/xcx/songame",
            {
                "game_uuid": game_uuid,
                "son_game_uuid": son_game_uuid,
                "stored_money": 1,
                "raffle_count": 1,
            },
            content_type="application/json",
        )
        raffle_res = json.loads(raffle_r.content)
        self.assertEqual(raffle_res["code"], 0)

        raffle_r = self.c.post(
            "/api/yifanshang/xcx/songame",
            {
                "game_uuid": game_uuid,
                "son_game_uuid": son_game_uuid,
                "stored_money": 1,
                "hunjing": 1,
                "raffle_count": 3,
            },
            content_type="application/json",
        )
        raffle_res = json.loads(raffle_r.content)
        self.assertEqual(raffle_res["code"], 0)

        raffle_r = self.c.post(
            "/api/yifanshang/xcx/songame",
            {
                "game_uuid": game_uuid,
                "son_game_uuid": son_game_uuid,
                "hunjing": 1,
                "score": 1,
                "raffle_count": 10,
            },
            content_type="application/json",
        )

        raffle_res = json.loads(raffle_r.content)
        print(raffle_res, "111222")
        self.assertEqual(raffle_res["code"], 0)

        raffle_r = self.c.post(
            "/api/yifanshang/xcx/songame",
            {
                "game_uuid": game_uuid,
                "son_game_uuid": son_game_uuid,
                "hunjing": 1,
                "score": 1,
                "raffle_count": "all",
            },
            content_type="application/json",
        )
        raffle_res = json.loads(raffle_r.content)
        self.assertEqual(raffle_res["code"], 0)


class XcxYfsSonGameDetail(TestCase):
    def setUp(self):
        self.c = Client(HTTP_X_WX_OPENID="wx123")
        self.factory = RequestFactory()

    @classmethod
    def create_a_user(cls):
        d = {
            "nickname": "xiaoming",
            "head_url": "http://nick",
            "openid": "wx123",
            "stored_money": 1000000,
            "hunjing": 100000,
            "score": 0,
        }
        u = Users(**d)
        u.save()
        return u

    def test_songame_detail(self):
        _, son_game_uuid = save_data()

        raffle_r = self.c.get(
            "/api/yifanshang/xcx/songame/detail",
            {
                "game_uuid": son_game_uuid,
            },
            content_type="application/json",
        )
        raffle_res = json.loads(raffle_r.content)
        self.assertEqual(raffle_res["code"], 0)
